package com.polaris.live.dialog

import android.content.Context
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.XPopupCallback
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.activity.BaseVmActivity
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.DialogManageBanBinding
import com.polaris.live.databinding.DialogManageMuteBinding
import com.polaris.live.databinding.DialogToolCardSureBinding
import com.polaris.live.interface_bag.XpopupCallBackImpl
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.ResourceOptionBaseVo
import com.polaris.live.resp.back_resp.UserBackpackConsumableResp
import com.polaris.live.resp.back_resp.UserBanOptionsResp
import com.polaris.live.resp.back_resp.UserConsumableDailyRewardResp
import com.polaris.live.resp.back_resp.UserMuteOptionsResp
import com.polaris.live.resp.go_resp.ManagerBanRequest
import com.polaris.live.resp.go_resp.ManagerMuteRequest
import com.polaris.live.resp.go_resp.ManagerUnMuteRequest
import com.polaris.live.translation.R.*
import com.polaris.live.utils.image.ImageUtils

/**
 * ManageBanDialog 封号
 *
 * @author Created by 天晴 on 2025/1/15/016 19:40
 */
class ManageBanDialog(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val otherId: Long,
    private val item: UserBanOptionsResp,
    private val banBack: (ban: Boolean) -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_manage_ban
    }

    private lateinit var mBinding: DialogManageBanBinding
    private var nowTimeType: ResourceOptionBaseVo? = null
    private var nowReasonType: ResourceOptionBaseVo? = null

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogManageBanBinding.bind(popupImplView)
        item.banDuration.getOrNull(0)?.let {
            setTimeData(it)
        }
        item.banReason.getOrNull(0)?.let {
            setReasonData(it)
        }
        mBinding.selectCl.setOnSingleClickListener {
            ManageSelectDialog.show(context, mBinding.selectCl, item.banDuration) {
                setTimeData(it)
            }
        }
        mBinding.selectReasonCl.setOnSingleClickListener {
            ManageSelectDialog.show(context, mBinding.selectReasonCl, item.banReason) {
                setReasonData(it)
            }
        }
        mBinding.tvSure.setOnSingleClickListener {
            managerMuteRequest()
        }
        mBinding.tvCancel.setOnSingleClickListener {
            dismiss()
        }
    }

    private fun setTimeData(data: ResourceOptionBaseVo) {
        nowTimeType = data
        mBinding.timeTv.text = data.name

    }

    private fun setReasonData(data: ResourceOptionBaseVo) {
        nowReasonType = data
        mBinding.reasonTv.text = data.name

    }

    private fun managerMuteRequest() {
        if (nowTimeType != null && nowReasonType != null) {
            val remark = mBinding.editTv.text.toString()
            if (remark.isNullOrEmpty()) {
                ToastGravity.showGravity(StringUtils.getString(string.please_enter_remarks))
                return
            }
            val request = ManagerBanRequest(nowTimeType!!.type, nowReasonType!!.type, otherId, remark)
            mViewModel.requestLoading({ userService.managerBan(request) }, {
                ToastGravity.showGravity(StringUtils.getString(string.ban_successfully))
                banBack.invoke(true)
                dismiss()
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }

    }


    companion object {
        fun show(
            context: Context,
            mViewModel: BaseViewModel,
            otherId: Long,
            onEditShow: (show: Boolean) -> Unit,
            banBack: (ban: Boolean) -> Unit,
        ) {
            mViewModel.requestLoading({ userService.managerBanOptions() }, {
                XPopup.Builder(context)
                    .isViewMode(true)
                    .moveUpToKeyboard(true)
                    .setPopupCallback(object : XpopupCallBackImpl {
                        override fun onDismiss(popupView: BasePopupView) {
                            super.onDismiss(popupView)
                            onEditShow.invoke(true)
                        }

                        override fun onCreated(popupView: BasePopupView) {
                            super.onCreated(popupView)
                            onEditShow.invoke(false)
                        }
                    })
                    .isDestroyOnDismiss(true)
                    .asCustom(ManageBanDialog(context, mViewModel, otherId, it, banBack))
                    .show()

            })
        }
    }
}